//
//  TextView.m
//  123
//
//  Created by 王云 on 16/11/21.
//  Copyright © 2016年 王云. All rights reserved.
//

#import "TextView.h"
#import <Masonry.h>

#define kWidth  [UIScreen mainScreen].bounds.size.width


// ------------ FieldView
@interface FieldView : UIView<UITextFieldDelegate>

@property (nonatomic,weak)UILabel *titleLabel;
@property (nonatomic,weak)UITextField *editLabel;
@property (nonatomic,weak)UIImageView *lineImg;
@property (nonatomic,copy)Click click;


@property (nonatomic,strong)TextModel *model;

@end

@implementation FieldView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"发票分类";
        titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:titleLabel];
        _titleLabel = titleLabel;
        UITextField *editLabel = [[UITextField alloc] init];
        [editLabel addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventEditingChanged];
        editLabel.placeholder = @"印刷品";
        editLabel.delegate = self;
        editLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:editLabel];
        _editLabel = editLabel;
        
        UIImageView *lineImg = [[UIImageView alloc] init];
        lineImg.backgroundColor = DarkColor;
        [self addSubview:lineImg];
        _lineImg = lineImg;
        
      
        __weak __typeof(&*self)weakSelf = self;
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).offset(8);
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(80, 30));
        }];
        [editLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.left.equalTo(titleLabel.mas_right).offset(8);
            make.right.equalTo(weakSelf.mas_right).offset(-8);
            make.height.equalTo(@39);
        }];
        [lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel.mas_left);
            make.right.equalTo(editLabel.mas_right);
            make.top.equalTo(editLabel.mas_bottom).offset(0);
            make.height.equalTo(@1);
        }];
    }
    return  self;
}

- (void)setModel:(TextModel *)model{
    _model = model;
    _click = _model.click;
    
    _titleLabel.text = _model.title;
    _editLabel.placeholder  =  _model.detailTitle;
    if (_model.actualT.length > 0) {
        _editLabel.text = model.actualT;
    }
}

#pragma mark - delegate
- (void)editClick:(UITextField *)field{
    if (_click) {
        _click(field.text);
    }
}
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    
//    if (_click) {
//        _click([NSString stringWithFormat:@"%@%@",textField.text,string]);
//    }
//    
//    return YES;
//}
//- (void)textFieldDidEndEditing:(UITextField *)textField{
//
//    if (_click) {
//        _click(textField.text);
//    }
//}
@end


#pragma mark - TextView
@interface TextView()

@property (nonatomic,strong)NSArray *data;

@property (nonatomic,assign)NSInteger prohibit;
@end

@implementation TextView

+ (TextView *)CreateArray:(NSArray *)Arr{
    
    TextView *text = [[TextView alloc] initWithFrame:CGRectMake(0, 0, kWidth, Arr.count*40)];
    text.data = Arr;
    return  text;
}
+ (TextView *)CreateArray:(NSArray *)Arr Prohibit:(NSInteger)prohibit{
    TextView *text = [[TextView alloc] initWithFrame:CGRectMake(0, 0, kWidth, Arr.count*40)];
    text.prohibit = prohibit;
    text.data = Arr;
    return  text;
}



- (void)setTitleWidth:(float)titleWidth{
    _titleWidth = titleWidth;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)setData:(NSArray *)data{
    _data = data;
    int i = 0;

    for (TextModel *model in _data) {
        FieldView *fiView = [[FieldView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
        fiView.model = model;
        if (_prohibit  == 1) {
            fiView.editLabel.userInteractionEnabled = NO;
        }
        [self addSubview:fiView];
        [fiView mas_makeConstraints:^(MASConstraintMaker *make) {
             make.size.mas_equalTo(CGSizeMake(kWidth, 40));
            make.left.equalTo(self.mas_left);
            make.top.mas_equalTo(40*i);
        }];
        i++;
    }
}

@end



//-----------textModel

@implementation TextModel

+ (TextModel *)Create:(NSString *)title detail:(NSString *)detailTitle actualT:(NSString *)actualT Click:(Click)click {
    
    TextModel *model = [[TextModel alloc] init];
    model.actualT = actualT;
    model.title = title;
    model.click = click;
    model.detailTitle = detailTitle;
    return model;
}


@end


